﻿$(document).ready(function () { bindPageInput();});

var currentSearchTerm = ''

function keyPageNum(event) {
    var pageNum = event.target.value;
    var intInput = parseInt(pageNum);
    if (isNaN(intInput)) return;
    var inputField = $("#" + event.target.id);
    var grid = inputField.parents('.tt_grid');
    var tableId = grid.attr('id');
    requestItems(tableId, intInput, -1);
}

//function keyPageSize(event) {
//    var pageNum = event.target.value;
//    var inputField = $("#" + event.target.id);
//    var grid = inputField.parents('.tt_grid');
//    var tableId = grid.attr('id');
//    var pageSizeInput = grid.find('input.GridPageSize').val(inputField.val());
//    requestItems(tableId, 1, -1);
//}
function bindPageInput() {
    $('.GridPageInput').bind('keypress', function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) {
            keyPageNum(e);
        }
    });
//    $('.GridPageSizeInput').bind('keypress', function (e) {
//        var code = (e.keyCode ? e.keyCode : e.which);
//        if (code == 13) {
//            keyPageSize(e);
//        }
//    });
}
function Items(pageNum, tableName) {
    requestItems(tableName, pageNum, -1);
}
function SortGrid(colNum, tableName) {
    var curPage = getCurrentPage(tableName);
    requestItems(tableName, curPage, colNum);
}
function updateItems(packetObj) {
    var body = $("#" + packetObj.TableName + ">tbody");
    body.html(packetObj.RowHTML);
    var foot = $("#" + packetObj.TableName + " tfoot");
    foot.html(packetObj.FooterHTML);
    var head = $("#" + packetObj.TableName + " thead");
    head.html(packetObj.HeaderHTML);
    var colgroup = $("#" + packetObj.TableName + " colgroup");
    colgroup.html(packetObj.ColgroupHTML);
    bindPageInput();
    $('#'+packetObj.TableName).trigger('gridPageChange');
    var inputField = $('#' + packetObj.TableName + ' input.GridPageInput');
    //setTimeout(function() { inputField.focus() }, 50);
}
function requestItems(tableName, pageNumber, sortColumn) {

    searchTerm = currentSearchTerm || '';

    var ajaxUrl = getAjaxUrl(tableName);
    var curSort = getCurrentSort(tableName);
    var pageSize = getPageSize(tableName);
    var parentId = $('#GridRefId').val();
    var ajaxData;
    if (parentId === null) {
        ajaxData = { gridName: tableName, pageNum: pageNumber, sortCol: sortColumn, currentSort: curSort, pageSize: pageSize, searchTerm: searchTerm };
    }
    else {
        ajaxData = { gridName: tableName, pageNum: pageNumber, sortCol: sortColumn, currentSort: curSort, pageSize: pageSize, id: parentId, searchTerm: searchTerm };
    }
    getAdditionalFields(tableName).each(function (n) {
        ajaxData[this.id] = this.value;
    });

    $.ajax({
        type: "post",
        data: ajaxData,
        url: ajaxUrl,
        success: updateItems,
        error: DisplayTrintechAjaxError,
       // async: false,
        cache: false
    });
}
function getAjaxUrl(tableName) {
    return $('#' + tableName + ' input.GridAjaxUrl').attr('value');
}
function getCurrentPage(tableName) {
    return $('#' + tableName + ' input.GridCurrentPage').attr('value');
}
function getCurrentSort(tableName) {
    return $('#' + tableName + ' input.GridSortColumns').attr('value');
}
function getPageSize(tableName) {
    return $('#' + tableName + ' input.GridPageSize').attr('value');
}
function getAdditionalFields(tableName) {
    return addFieldsDiv = $('#' + tableName + '_AdditionalFields input');
}
function refreshCurrentPageItems(tableName) {
    var currentPage = $('#' + tableName + ' input.GridCurrentPage').val();
    Items(currentPage, tableName);
}

function refreshCurrentPageItemsWithSearchTerm(tableName,searchTerm) {
    var currentPage = $('#' + tableName + ' input.GridCurrentPage').val();
    Items(currentPage, tableName, searchTerm);
}

function resetCurrentPageWithSearchTerm(tableName, searchTerm) {
    currentSearchTerm = searchTerm
    Items(1, tableName);
}




